import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart' as ip;
import 'package:image_pickers/image_pickers.dart';

/// <summary>
/// todo: 底部划出工具
/// author：zwb
/// dateTime： 2021/11/11 16:35
/// filePath： lib/widget/bottom_sheet_widget.dart
/// desc:
/// <summary>
///
class BottomSheetWidget{
  static final  ip.ImagePicker _picker = ip.ImagePicker();

  /// todo：选择相册、拍照
  static bottomImagePickWidget({
    Function(List<ImageVideoPickModel>)? callBack,
    int selectCount:1,
    bool model:true,
}){

    Get.bottomSheet(
        Material(
          color: Colors.transparent,
          child: Container(
            height: 210,
            color: Colors.white,
            padding: EdgeInsets.symmetric(vertical: 15),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                _cameraButton(callBack,typeModel: model),
                Divider(color: Colors.grey.shade300,),
                _photosButton(callBack,selectCount: selectCount,typeModel: model),
                Divider(color: Colors.grey.shade300,),
                _cancel(),
              ],
            ),
          ),
        )
    );
  }

  /// todo：相机按钮
  static _cameraButton(Function(List<ImageVideoPickModel>)? call,{typeModel}){
    return MaterialButton(onPressed: () async{
      List<ImageVideoPickModel> _imgs = [];
      final ip.XFile? photo;
      if(typeModel){
        photo = await _picker.pickImage(source: ip.ImageSource.camera,imageQuality: 45);
      }else{
        photo = await _picker.pickVideo(source: ip.ImageSource.camera,maxDuration: Duration(seconds: 15));
      }
      if(photo!=null){
        var date = await photo.lastModified();
        _imgs.add(ImageVideoPickModel(name: photo.name,path: photo.path,lastModified: date));
      }
      call!(_imgs);
    },minWidth: Get.width * 0.7,child: Text("拍 摄",style: TextStyle(color: Colors.green,fontSize: 16,fontWeight: FontWeight.bold),),);
  }

  /// todo：相册按钮
  static _photosButton(Function(List<ImageVideoPickModel>)? call,{int selectCount:1,bool typeModel:true}){
    return MaterialButton(onPressed: () async{
      List<ImageVideoPickModel> _imgs = [];
      List<Media> _listImagePaths = await ImagePickers.pickerPaths(
        galleryMode: typeModel ? GalleryMode.image : GalleryMode.video,
        selectCount: selectCount,
        showGif: false,
        showCamera: false,
        compressSize: 500,
        uiConfig: UIConfig(uiThemeColor: Color(0xff307b07)),
        cropConfig: CropConfig(enableCrop: false, width: 2, height: 1),
      );
      _listImagePaths.forEach((element) {
        _imgs.add(ImageVideoPickModel(name: element.path!.split("/").last,path: element.path,lastModified: File(element.path??'').lastModifiedSync()));
      });
      call!(_imgs);
    },minWidth: Get.width * 0.7,child: Text("相 册",style: TextStyle(color: Colors.green,fontSize: 16,fontWeight: FontWeight.bold),),);
  }

  /// todo：取消按钮
  static _cancel(){
    return MaterialButton(onPressed: () async{
      Get.back();
    },child: Text("取 消",style: TextStyle(color: Colors.green,fontSize: 16,fontWeight: FontWeight.bold),),);
  }
}

class ImageVideoPickModel{
  String? name;
  String? path;
  DateTime? lastModified;

  ImageVideoPickModel({this.name, this.path, this.lastModified});
}